import api from "@/api";
import Heroe from "@/object/Heroe";
import Tactic from "@/object/Tactic";
import Book from "@/object/Book";
import { Dialog } from "vant";

export default {
  namespaced: true,
  state: {
    testaaa: "初始",
    loginStatus: false, // 登录状态
    heroesList: [], // 武将列表
    userHeroes: [], // 用户拥有的武将

    tacticsList: [], // 战法列表
    userTactics: [], // 用户拥有战法

    booksList: [], // 兵书列表
    dataLoading: true, // 数据加载
    alldataLoading: true, // 数据加载
  },
  actions: {
    checkLogin({ state }) {
      return state.loginStatus;
    },
    async load({ state }) {
      let heroes = await api.HEROES_LIST(0, 10);
      state.heroesList = heroes.map((item) => new Heroe(item));

      let tactics = await api.TACTICS_LIST(0, 10);
      state.tacticsList = tactics.map((item) => new Tactic(item));

      let books = await api.BOOKS_LIST(0, 10);
      state.booksList = books.map((item) => new Book(item));

      try {
        let userHeroes = await api.GET_USER_HEROES();
        state.userHeroes = userHeroes || [];

        let userTactics = await api.GET_USER_TACTICS();
        state.userTactics = userTactics || [];

        window.loginStatus = true;
        state.loginStatus = true;
      } catch (err) {
        window.loginStatus = false;
        state.loginStatus = false;
        state.userHeroes = [];
        state.userTactics = [];
        console.log(err);
      }

      state.dataLoading = false;
      window.stateData = state;

      this.dispatch("data/syncGetData");
    },
    async syncGetData({ state }) {
      // 武将列表
      let heroes = await api.HEROES_LIST();
      state.heroesList = heroes.map((item) => new Heroe(item));

      // 战法列表
      let tactics = await api.TACTICS_LIST();
      state.tacticsList = tactics.map((item) => new Tactic(item));

      // 兵书列表
      let books = await api.BOOKS_LIST();
      state.booksList = books.map((item) => new Book(item));

      state.alldataLoading = false;
      window.stateData = state;
    },
    // 通过ID获取武将
    getHeroe({ state }, id) {
      let heroe = null;
      state.heroesList.some((item) => {
        if (item.id === id) heroe = item;
        return !!heroe;
      });
      return heroe;
    },
    // 通过ID获取战法
    getTactic({ state }, id) {
      let tactic = null;
      state.tacticsList.some((item) => {
        if (item.id === id) tactic = item;
        return !!tactic;
      });
      return tactic;
    },
    // 通过ID获取兵书
    getBook({ state }, id) {
      let book = null;
      state.booksList.some((item) => {
        if (item.id === id) book = item;
        return !!book;
      });
      return book;
    },
    async updateUserHeroe({ state }, ids) {
      await api.UPDATE_USER_HEROES(ids.join(","));
      state.userHeroes = [...ids];
    },
    // 设置用户拥有武将  已有就取消  没有就添加
    async setUserHeroe({ state }, id) {
      if (!window.token) {
        return Dialog.alert({
          className: "download-alert",
          title: "武将招募信息仅可在Ganke中记录",
          theme: "round-button",
          confirmButtonText: window.from !== "vivo" ? "下载GankeAPP" : "确认",
          confirmButtonColor: "#FF8A69",
          closeOnClickOverlay: true,
        }).then(() => {
          window.injectedAll.download();
        });
      }

      let status = state.userHeroes.indexOf(id) === -1 ? 1 : 0;
      try {
        await api.SET_USER_HEROES(id, status);
        if (state.userHeroes.indexOf(id) === -1) {
          state.userHeroes.push(id);
        } else {
          state.userHeroes.splice(state.userHeroes.indexOf(id), 1);
        }
        window.stateData = state;
      } catch (err) {
        window.injectedAll.openLogin(); // 跳转登录
      }
    },
    // 设置用户拥有战法  已有就取消  没有就添加
    async setUserTactics({ state }, id) {
      if (!window.token) {
        return Dialog.alert({
          className: "download-alert",
          title: "战法演练信息仅可在Ganke中记录",
          theme: "round-button",
          confirmButtonText: window.from !== "vivo" ? "下载GankeAPP" : "确认",
          confirmButtonColor: "#FF8A69",
          closeOnClickOverlay: true,
        }).then(() => {
          window.injectedAll.download();
        });
      }

      let status = state.userTactics.indexOf(id) === -1 ? 1 : 0;
      try {
        await api.SET_USER_TACTICS(id, status);
        if (state.userTactics.indexOf(id) === -1) {
          state.userTactics.push(id);
        } else {
          state.userTactics.splice(state.userTactics.indexOf(id), 1);
        }
        window.stateData = state;
      } catch (err) {
        window.injectedAll.openLogin(); // 跳转登录
      }
    },
  },
  /**
   * mutations 通过 this.$store.commit 来调用
   * 专注于修改 state 不能异步 state相关的操作尽量在 mutations中完成
   */
  mutations: {},
};
